package com.star.web.order.controller;

import com.star.entity.Result;
import com.star.order.feign.CartFeign;
import com.star.order.feign.OrderFeign;
import com.star.order.pojo.Order;
import com.star.order.pojo.OrderItem;
import com.star.user.feign.AddressFeign;
import com.star.user.pojo.Address;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/*
			  ________   ___   ___        ___   ________   ___   ___        ___
	 ________|\   __  \_|\  \_|\  \______|\  \_|\   __  \_|\  \_|\  \______|\  \_________
	 \       \ \  \_\ /_\ \  \\ \  \     \ \  \\ \  \_\ /_\ \  \\ \  \     \ \  \        \
	  \       \ \   __  \\ \  \\ \  \     \ \  \\ \   __  \\ \  \\ \  \     \ \  \        \
	   \       \ \  \_\  \\ \  \\ \  \____ \ \  \\ \  \_\  \\ \  \\ \  \____ \ \  \        \
	    \       \ \_______\\ \__\\ \_______\\ \__\\ \_______\\ \__\\ \_______\\ \__\        \
	     \       \|_______| \|__| \|_______| \|__| \|_______| \|__| \|_______| \|__|         \
	 	  \___________________________________________________________________________________\

*/
@Controller
@RequestMapping("/worder")
public class OrderController {

    @Autowired
    private AddressFeign addressFeign;
    @Autowired
    private CartFeign cartFeign;

    @Autowired
    private OrderFeign orderFeign;

    /**
     * 订单结算页面渲染
     * @param model
     * @return
     */
    @GetMapping("/ready/order")
    public String readyOrder(Model model) {
        //收件人地址信息
        List<Address> addressList = addressFeign.list().getData();
        model.addAttribute("address", addressList);
        //购物车信息
        Map map = cartFeign.list();
        //获取购物车商品列表
        List<OrderItem> orderItemList = (List<OrderItem>) map.get("orderItemList");
        //总金额
        Integer totalMoney = (Integer) map.get("totalMoney");
        Integer totalNum = (Integer) map.get("totalNum");

        //将数据传入前端页面
        model.addAttribute("carts",orderItemList);
        model.addAttribute("totalMoney",totalMoney);
        model.addAttribute("totalNum",totalNum);
        //加载默认收件人信息
        for (Address address : addressList) {
            if (address.getIsDefault().equals("1")) {
                //默认收件人
                model.addAttribute("deAddr", address);
                break;
            }
        }
        return "order";
    }

    /**
     * 添加订单信息
     * @param order
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Result add(@RequestBody Order order) {
        Result result = orderFeign.add(order);
        return result;
    }

    /**
     * 跳转支付页面
     * @param orderId 订单id
     * @return 支付页面
     */
    @GetMapping("/toPayPage")
    public String toPayPage(String orderId,Model model) {
        //获取订单相关信息
        Order order = orderFeign.findById(orderId).getData();
        model.addAttribute("orderId", orderId);
        //支付金额
        model.addAttribute("payMoney", order.getPayMoney());
        return "pay";
    }
}
